//
//  TTHomeViewModel.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/8.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit
import YYModel
import SDWebImage
class TTHomeViewModel: NSObject {

    var listArray:[TTStatuesViewModel] = [TTStatuesViewModel]()
    
    //获取数据
    func setHomeDate(isup:Bool,isscuees:@escaping (Bool,String)->()){
    
   
        
    if let token = TTHMUserAccountViewModel.sharedAccountModel.accountModel?.access_token
    {
        
        
        
        var maxid:Int64 = 0
        var sinceid:Int64 = 0
        
        if isup{
        
            maxid = listArray.last?.statues?.id ?? 0
            if maxid > 0{
            maxid = maxid - 1
            }
        }else
        {
        sinceid = listArray.first?.statues?.id ?? 0
        
        }
        
        var msagee = "没有更多数据"
        TTToolsManager.sharedManager.attentionFrome(token: token, maxid: maxid, sinceid: sinceid, success: { (project, error) in
            
            
            
            if error != nil
            {
            
                isscuees(false,msagee)
                print(error)
                return
            
            }
           //字典转模型
            let tempProject = project as! [String:Any]
            let tempArray = tempProject["statuses"] as! [TTStatusesModel]
            var modelArray = [TTStatusesModel]()
             modelArray = NSArray.yy_modelArray(with: TTStatusesModel.self, json: tempArray) as! [TTStatusesModel]
            var stusArray = [TTStatuesViewModel]()
            //遍历模型数组,把模型赋值给stusViewModel
            for model in modelArray
            {
                let stusViewModel = TTStatuesViewModel()
                stusViewModel.statues = model
                
                stusArray.append(stusViewModel)
                
                let gruop = DispatchGroup()
                //判断是否是一张图片
                if model.pic_urls?.count == 1
                {
                    //  图片地址
                    let imageUrl = model.pic_urls!.first!.thumbnail_pic!
                    //  把任务添加到调度组里面
                    gruop.enter()
                    //  原创微博配图个数为1，需要下载这个单张图片
                    SDWebImageManager.shared().loadImage(with: URL(string: imageUrl), options: [], progress: nil, completed: { (image, imageData, error, _, _, url) in
                     
                        //  图片下载完成，从调度组里面离开
                        gruop.leave()
                    })
                }
                if model.retweeted_status?.pic_urls?.count == 1
                {
                
                    let imageUrl = model.retweeted_status!.pic_urls!.first!.thumbnail_pic!
                    //  把任务添加到调度组里面
                    gruop.enter()
                    //  转发微博配图个数为1，需要下载这个单张图片
                    SDWebImageManager.shared().loadImage(with: URL(string: imageUrl), options: [], progress: nil, completed: { (image, imageData, error, _, _, url) in
                        
                        //  图片下载完成，从调度组里面离开
                        gruop.leave()
                    })
                
                }
                
            }
            
          
            
            if isup{
            
             self.listArray.append(contentsOf: stusArray)
            }else
            {
            self.listArray.insert(contentsOf: stusArray, at: 0)
            }
            
            if stusArray.count > 0
            {
            msagee = "加载了\(stusArray.count)条数据"
            
            }
        
            DispatchQueue.main.async {
               isscuees(true,msagee)
            }
           
    
        })
        
        
        
    }
        
        
    }
    
    
}
